package com.rj.bd.admin.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.rj.bd.admin.dao.AdminMapper;
import com.rj.bd.admin.entity.Admin;
import com.rj.bd.admin.entity.JiLu;
import com.rj.bd.covrersion.entity.Coversion;
import com.rj.bd.shop.entity.Shop;
import com.rj.bd.user.entity.User;

import cn.hutool.core.math.Money;
import cn.hutool.core.util.IdUtil;

@Transactional
@Service("adminService")
public class AdminServiceImpl implements IAdminService {
	@Autowired
	private AdminMapper adminMapper;

	/**
	 * @desc 管理员模块的登录和验证
	 */
	public Map<String, Object> login(String name, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		Admin admin = adminMapper.selectByAdmin(name);
		if (admin == null) {
			map.put("code", "-1");
			map.put("msg", "请先去注册");
			return map;
		} else {
			String password1 = admin.getPassword();
			if (password.equals(password1)) {
				map.put("code", "200");
				map.put("msg", "登录成功");
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("token", admin.getToken());
				map.put("data", data);
				return map;
			} else {
				map.put("code", "-1");
				map.put("msg", "密码错误");
				return map;
			}
		}
	}

	/**
	 * @desc 查询所有用户
	 */
	public Map<String, Object> queryUser() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> list = adminMapper.findByUser();
		for (User user : list) {
			user.setCaozuo("<a class='btn btn-secondary' data-id=" + user.getUserId() + " href=money.html?userId="
					+ user.getUserId() + ">修改</a>");
		}
		map.put("code", "200");
		map.put("msg", "查询成功");
		map.put("data", list);
		return map;
	}

	/**
	 * @desc 查询商品记录表
	 */
	public Map<String, Object> queryShop() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Shop> list = adminMapper.findByShop();
		map.put("code", "200");
		map.put("msg", "查询成功");
		map.put("data", list);
		return map;
	}

	/**
	 * @desc 查询商品
	 */
	public Map<String, Object> queryConversion() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Coversion> list = adminMapper.findCoversion();
		map.put("code", "200");
		map.put("msg", "查询成功");
		map.put("data", list);
		System.out.println(list);
		return map;
	}

	/**
	 * @desc 添加用户
	 */
	public Map<String, Object> addUser(String username, String password, String name, String sex, String tel,
			String money) {
		Map<String, Object> map = new HashMap<String, Object>();
		String userId = IdUtil.simpleUUID();
		//String userId=i.toString();
		String token = IdUtil.simpleUUID();
		int adduser = adminMapper.addUser(userId, username, password, name, sex, tel, money, token);
		map.put("code", "200");
		map.put("msg", "添加成功");
		return map;
	}

	/**
	 * @desc 查询购买记录
	 */
	public Map<String, Object> queryShopJilu() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<JiLu> list = adminMapper.queryShopJilu();
		map.put("code", "200");
		map.put("msg", "查询成功");
		map.put("data", list);
		System.out.println(list);
		return map;
	}
	/**
	 * @desc 修改用户积分
	 */
	public Map<String, Object> updateUserMoney(String money,String userId) {
		Map<String, Object> map=new HashMap<String, Object>();
		int update=adminMapper.updatemoeny(money,userId);
		map.put("coed", "200");
		map.put("msg", "修改成功");
		return map;
	}

	public List<JiLu> exportExcelInfo() {
		return adminMapper.queryShopJilu();
	}

	public List<JiLu> allJiLu() {
		return adminMapper.queryShopJilu();
	}

/*	public int importExcel(HttpServletRequest request, HttpServletResponse response, MultipartFile upload) {
		return adminMapper.importExcel(shopId, userId, conId, shopTime, address);
	}
*/
}
